package com.yunpuvip.sachet.modular.system.factory;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.*;

public class DataFactory {
    /**
     * 图片路径json to List</String>
     * @param jsonStr
     * @return
     */
    public  static List<String> jsonStrToList(String jsonStr){
        if(ObjectUtil.isNull(jsonStr)|| StrUtil.isBlank(jsonStr)){
            return new ArrayList<String>();
        }
        JSONArray array = JSONUtil.parseArray(jsonStr);
        List<String> stringList = JSONUtil.toList(array, String.class);
        return stringList;
    }


    /**
     *
     *
     * 手动将list按分页拿数据
     */
    public static Page getDataBypage(Page page, List list){
        List dataList = Collections.emptyList();
        if(list.size()<=0){
            page.setTotal(0);
            return page;
        }
        Long begin=(page.getCurrent()-1)*page.getSize();
        Long end=page.getSize()*page.getCurrent();
        System.err.println("begin"+begin);
        System.err.println("end"+end);
        long index = -1;
        if(begin.intValue()>list.size()){
            return page.setRecords(dataList);
        }
        //判断是否越界
        List subList = list.subList(begin.intValue(), list.size() > end.intValue() ? end.intValue() : list.size());
        page.setRecords(subList);
        page.setTotal(list.size());
        return page;
    }


    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        int size = list.size();
        System.out.println(list.subList(0,3));


    }



}
